/*
  KeePass Password Safe - The Open-Source Password Manager
  Copyright (C) 2003-2011 Dominik Reichl <dominik.reichl@t-online.de>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "StdAfx.h"
#include "PopularPasswords.h"
#include <algorithm>
//#include "MemUtil.h"
#include "StrUtil.h"
#include "../../Common/Crypto/SHA2/SHA2.h"

// Bloom filter-based popular password checking.

static const int PPC_TABLE_SIZE = 8192 * 8; // Bits, multiple of 64

// Bits set: 32433 of 65536
// Hash functions: 32
// Phi (bits unset ratio) estimation: 0.505455388896019
// Exact Phi: 0.505111694335938
// False positives ratio: 1.67583063859565E-10
static const UINT64 PPC_TABLE[PPC_TABLE_SIZE / 64] = {
	0x60383D3A85560B9Bui64, 0x2578CE9D37C6AEB7ui64, 0xF509A5743FD03228ui64,
	0x19B7455E8933EE56ui64, 0x5EA419ADCFD9C20Eui64, 0xEA618EFC0B37A162ui64,
	0xE0FD4D1FFF1CE415ui64, 0x7A649E0301BB6060ui64, 0x80D9CD9F9EEB603Dui64,
	0x47D6010D0D6E6CDEui64, 0x2552708C589EB554ui64, 0x073F1A3DB3267502ui64,
	0x3313FEC2A2FEA475ui64, 0x4593665C44934FEBui64, 0x410A301A23660395ui64,
	0x6AD06DA533FF5659ui64, 0x423DAF86F3E41F4Aui64, 0x82F035A971C6FD18ui64,
	0xB5E9139F28C93223ui64, 0x1D07C3F4160585CAui64, 0x24B01EDB6B23E2C5ui64,
	0xD52F25B724F936C9ui64, 0x8018392517836928ui64, 0x3AA4C0F8E181EDA2ui64,
	0x8D93683EF7D52529ui64, 0x6164BB6208114460ui64, 0x737A04D8FEF3D88Fui64,
	0x3400097098D5C2CBui64, 0x3C2B9ABE5C455B2Eui64, 0x3A3819973AB32DA2ui64,
	0x38ACB428510AF40Bui64, 0x83320D5114B74771ui64, 0xC25BEC333B90DCD1ui64,
	0x0E9F412FBA3813D1ui64, 0x047E31E3098EB2B8ui64, 0xBB686AC643F1741Fui64,
	0x0BE22E9C0EF0E8F2ui64, 0x65AA9504E5F40D31ui64, 0xE018DF5D64C62AC7ui64,
	0x17020E9A7EFA12EDui64, 0xFC12A7C16006DE82ui64, 0x8DE4747E3745346Dui64,
	0x31D8C051A43CECAFui64, 0xBE9AFBEF127C1B12ui64, 0xAEE94B4B808BBEE2ui64,
	0x3A0099CA32835B41ui64, 0x59EB3173468D8C49ui64, 0x6F89DB1E6DAAE9E1ui64,
	0x4C1ADAA837E968E4ui64, 0x6E3593A56C682769ui64, 0x022AD591689B5B82ui64,
	0x4AC33861ED978032ui64, 0xF6F476E4E6A1318Dui64, 0x2DA690A11AA05A23ui64,
	0x916FC56378C29D77ui64, 0xAB3238BE22294659ui64, 0x2D73A29019B28C77ui64,
	0xAAF26C12EC9C3C42ui64, 0x058A278A24B334F9ui64, 0x033BD18FB8D9BACDui64,
	0x8B3833596008B07Cui64, 0x280B6D093333E5E5ui64, 0x2128DBE126CA3E1Eui64,
	0xCCF09769382472D8ui64, 0x0CB6E495BD90CED6ui64, 0x1303A37577C01C5Aui64,
	0xC8BBF4734FC34C53ui64, 0x1B38B72B10F86CD5ui64, 0x5098E2D6C1892E51ui64,
	0x2DD8065B79DB5380ui64, 0x5B9A1A6D6A2292B7ui64, 0xC70F751604D0497Cui64,
	0x911E08D7363B5213ui64, 0x9F2E245273308D2Eui64, 0x64D354827957F50Eui64,
	0x09856750F560342Cui64, 0xDE091F26603F0E70ui64, 0xDDE6B4E76173E3B1ui64,
	0xC1584AE1B26FA08Eui64, 0x1EA29887837838D2ui64, 0x6D7643FC67B15C54ui64,
	0x921E60571ED103EAui64, 0x63EB1EB33E7AFFF1ui64, 0x80BA4D1F95BFD615ui64,
	0xEC8A1D4FC1A6B8E0ui64, 0x2C46861B6DB17D1Aui64, 0x01F05D06927E443Bui64,
	0x6172EC2EABEAD454ui64, 0x21B8726C6F7C4102ui64, 0x3C016CD9945C72ECui64,
	0x708F77B2C0E8B665ui64, 0xFC35BE2BB88974DAui64, 0x805897A33702BD61ui64,
	0x9A93367A6041226Cui64, 0xFDAB188B6158F6BEui64, 0x5F21014A065E918Cui64,
	0xF4381DD77772D19Cui64, 0xC664B6358AA85011ui64, 0xF2639D7B3E2307E6ui64,
	0x3FA000D4A5A9C37Aui64, 0x8F45D116ED8DC70Fui64, 0x8CB8758E45C140D0ui64,
	0x49832B46D716236Dui64, 0xCC8E4961A93065B8ui64, 0x8A996533EDACEB0Eui64,
	0x15B35155EC56FAC1ui64, 0xE7E0C6C05A9F1885ui64, 0x05914F9A1D1C79F9ui64,
	0x730000A30B6725F0ui64, 0xC95E671F8E543780ui64, 0x47D68382400AF94Eui64,
	0x1A27F2734FE2249Aui64, 0x828079C332D9C0ABui64, 0x2E9BC798EA09170Eui64,
	0x6B7CDAC829018C91ui64, 0x7B89604901736993ui64, 0xABE4EB26F47608F0ui64,
	0x70D5FDC88A0FF1B1ui64, 0x5A1F0BAB9AB8A158ui64, 0xDC89AE0A735C51A4ui64,
	0x36C1EA01E9C89B84ui64, 0x3A9757AF204096DBui64, 0x1D56C8328540F963ui64,
	0x910A8694692472FAui64, 0x697192C9DF145604ui64, 0xB20F7A4438712AA2ui64,
	0xE8C99185243F4896ui64, 0xFBC8970EDBC39CA7ui64, 0x33485403868C3761ui64,
	0xAFA97DDEDB1D6AD8ui64, 0x54A1A6F24476A3BBui64, 0xFE4E078B184BDB7Fui64,
	0x5ED1543919754CD8ui64, 0x86F8C775160FC08Cui64, 0x9B4098F57019040Dui64,
	0x039518BBE841327Bui64, 0x111D0D420A3F5F6Aui64, 0x0666067346AF34ACui64,
	0xD43F1D14EB239B9Bui64, 0xA6BB91FEB5618F5Bui64, 0xA2B5218B202409ADui64,
	0xC004FA688C3AC25Eui64, 0xF0E2D9EA2935E1DCui64, 0x380B31CFA2F2AF43ui64,
	0x50E050AE426250EAui64, 0x628ED94D1AA8F55Bui64, 0xF8EB0654F0166311ui64,
	0x1F8858D26DDA5CC5ui64, 0x931425D11CB1EFEBui64, 0xF661D461DC1A05D3ui64,
	0x7B75ED7EC6936DA8ui64, 0x8713C59690985202ui64, 0xF61D6F93F07C0E85ui64,
	0xFD1771F6711D6F4Fui64, 0x5835A67E1B11419Fui64, 0x33EF08ABD56A1050ui64,
	0x55B5D0043FA2C01Cui64, 0x53316ED963B92D9Dui64, 0x6A8C93744E521EDBui64,
	0x083E948062EB9543ui64, 0x1C15289B3189AFB1ui64, 0xA6A0A5053AE2212Dui64,
	0x6573AF7F01FAFF3Bui64, 0x58B6F034CFCFE843ui64, 0xEB2837CA5AEA6AEDui64,
	0x633E7897097AC328ui64, 0x7FA91789B6CCEE82ui64, 0xBEE2402F4E7D65EEui64,
	0x616A103EC8FB0DBEui64, 0x65991F9FB25E13FCui64, 0x54EA8A3FADEC1F4Bui64,
	0x6D497C5ACDEA0E7Aui64, 0x5865045E8CA18527ui64, 0xA406C09215ABD61Fui64,
	0x68F81F5745FC9875ui64, 0xE496D850CEFF3FA9ui64, 0xD225C88D63212CB1ui64,
	0x37676390525116D2ui64, 0x415614AB14188A7Dui64, 0xABE58EBC1F6DDC63ui64,
	0xDE10312B2C25D28Cui64, 0x86C86D7A0B847635ui64, 0x408B511D584DC3DCui64,
	0x6711FCC14B303FEDui64, 0x1284DF9CC6972023ui64, 0xC3CE0B33141BFA8Fui64,
	0x0F3F58367D4A1819ui64, 0x9313F83058FBE6D0ui64, 0x6FCA5EF39B8E2F47ui64,
	0xA90F5C95D887756Dui64, 0x96C4E2AD85D5AF6Eui64, 0x0ED68A81F526F0A0ui64,
	0x53E4472DB4255A35ui64, 0xAC581015134D58A6ui64, 0x12C000D85C644FC7ui64,
	0x124D489B2C0FE6E4ui64, 0x8FF83531C6F5D61Aui64, 0x132BD6488304F73Bui64,
	0x110E99BC59604CB9ui64, 0xC28186ACBC940C9Bui64, 0x2094C07F48141230ui64,
	0x65FB9881A5053589ui64, 0x940A3E6D72F09D69ui64, 0x972A922CB14BA66Eui64,
	0x8D07E59C6DDD4327ui64, 0xCB67F993F820157Cui64, 0x65B7A54E5FB2ED6Cui64,
	0xC235828849576653ui64, 0xA695F85479467538ui64, 0x9E2BA885E63C4243ui64,
	0xDE64A6A5EF84C222ui64, 0xC2AB9AF302080621ui64, 0x88DBA09B87FA0734ui64,
	0xDF002765B44D02E1ui64, 0xD50D8D90587CD820ui64, 0x1B68B70ED179EFE1ui64,
	0xD6E77F8EC26AE95Cui64, 0xEE57EB7C45051872ui64, 0x4D2B445F36A7F9FDui64,
	0x5502ABB8BB14D7F1ui64, 0xAF2C0DF0406FA901ui64, 0x6522833444BF4A83ui64,
	0xD7CB2E3FC691BE8Dui64, 0x4F36F70D2E80D19Aui64, 0xF6945FE911D4923Bui64,
	0xE3C6FE1EA47399C1ui64, 0xF09EA1B2F837702Cui64, 0x5122537CF97B5CB5ui64,
	0x0C8202B70E9BF154ui64, 0x68B554AB58EB5E68ui64, 0x7BF9B8052C9BEADEui64,
	0x33612BFCD303810Dui64, 0x03E38CF67B37DC53ui64, 0x2BFDFF8691F37D5Cui64,
	0x4AB483D1CB1D07F6ui64, 0xF071A58640639A5Cui64, 0x9D6B98169B745CE1ui64,
	0x5F42D3E870FDCD93ui64, 0x4EDF04404F258238ui64, 0x2EAB6E10D65C9BB3ui64,
	0x5BB71411EF78DAD2ui64, 0x0DE8128636A5D689ui64, 0x18FDD1F484DC9365ui64,
	0x9896B8896941DA5Bui64, 0x8BEF8E3BA4448E5Fui64, 0x963A1E977CB1D2CAui64,
	0x02BCF5F068D52851ui64, 0x0CD783F09BFBE381ui64, 0x350DA833D8C5DB47ui64,
	0x8D444C914D795C43ui64, 0x8A00B4DFC44D9476ui64, 0x4B36CBEC089E55FDui64,
	0xD9D2FA1B0AC66211ui64, 0x6C7FC30FA31A8B60ui64, 0x9EF4504CC985AD6Bui64,
	0x8F2E7E5E0C00EE73ui64, 0x819131CFEEBEA069ui64, 0xB1E406A863E7A1B4ui64,
	0x501F072FF1F2AB67ui64, 0xDE578BFC5ADBC264ui64, 0xCDD66A09C8E13881ui64,
	0x4D443460CE52957Fui64, 0x3B198C267976ECFAui64, 0x6B98323D8BD26522ui64,
	0x80161F6A489E4BF8ui64, 0xE03A8AFCC7AE6872ui64, 0x2484BD95A305AB27ui64,
	0x6ADDAA46BF25DD0Eui64, 0xA429D8B00100477Cui64, 0x55AEDB88A074BF2Cui64,
	0x63D9F9021AB8F5F3ui64, 0x37858538A10C265Cui64, 0xEF54C2CE9D063149ui64,
	0xFA5CE5AF33E2C136ui64, 0xE601A559D0C391D7ui64, 0x7C4ED29BBF57DC7Eui64,
	0x8FD0D4146DE9E900ui64, 0xB58ABFA6CE6C0733ui64, 0xF8D7F7743B33EAFFui64,
	0x453FA782F454643Cui64, 0xD01752C21AF21E66ui64, 0xA50BB7913EAF05DFui64,
	0x966D5B140B2F4189ui64, 0x956F5638AFF3D148ui64, 0x93FAA838420E8AB3ui64,
	0x715E26043071EABDui64, 0x01E7B458B5FD3A41ui64, 0x5CFA99C4CC0492AAui64,
	0x761FD391C3623044ui64, 0xD39E44E9DB96B5BCui64, 0x8806C544F0534A07ui64,
	0x9B225CAFE97EAAC1ui64, 0xEAE5E18583492767ui64, 0x6B4E51E4C297F096ui64,
	0xFC512662EF47E41Dui64, 0xB6AC60427DB46F8Bui64, 0x8F137F3DB4429C9Dui64,
	0x04C65FBEAE9FD8D0ui64, 0xEB72305958AE5022ui64, 0xAA93AA14BCA2961Eui64,
	0x6C7547F9456CA37Aui64, 0xEE6094871615BA34ui64, 0x489BC8EDE0940402ui64,
	0x1108AEFAAD892229ui64, 0x673B8B1CF6BED23Eui64, 0xFDB781A75FD94DEAui64,
	0x11D9E0F5D914A7BEui64, 0x02830D07F018143Dui64, 0x9B3163B8188FD2BAui64,
	0x32C1BEC97D06117Eui64, 0x697268B761240CFFui64, 0xBD89CE3037C2E7A9ui64,
	0xF21C158125B19600ui64, 0x632CB1931601B70Aui64, 0x7BB3FB131338085Cui64,
	0xA9C06689B8138384ui64, 0x161CCBF83EBDC2A1ui64, 0x2CF83C01A80B7935ui64,
	0x9E51FE393B8E2FF0ui64, 0xFE96E52B1606C1A7ui64, 0x5E20DFB87F81ACCEui64,
	0xF95DB9602CDAE467ui64, 0xDEA155CD35555FEBui64, 0xF0669B810F70CDC6ui64,
	0xD36C2FBEB6A449ACui64, 0xCE500C6621C0A445ui64, 0x41308909E366460Aui64,
	0xAC4D8178DA0CEC24ui64, 0xC69049179ED09F7Dui64, 0x36B608A0BA2FD848ui64,
	0xDF511894DD9568B4ui64, 0xB3BFDF78EC861A6Cui64, 0xCD50F39D19848153ui64,
	0xD2C1BC57E78A408Cui64, 0x1E6613EFBB11B5EBui64, 0xF58E30D2D90F73D3ui64,
	0xCCB5E2F5E168D742ui64, 0xEE97259469BDB672ui64, 0x6784D35AF65935A8ui64,
	0x71032765ADED1FE8ui64, 0x4BBF2FE54D9B72E3ui64, 0x5A1BB7831E876A05ui64,
	0x12A8FC949EE91686ui64, 0x8296F8FA83BD112Cui64, 0xAAA7E3BFF64D34D5ui64,
	0x0301655E1794EE4Bui64, 0x1E547C011BBF30E1ui64, 0x39D74FEC536F31D6ui64,
	0x3C31A7478B1815BAui64, 0x525C774F82D5836Eui64, 0xECF7186DC612FD8Cui64,
	0x96B7C4EDD1F3536Fui64, 0x7E8C21F19C08541Cui64, 0xEE92DB0CF91E4B09ui64,
	0xF666190D1591AE5Dui64, 0x5E9B45102C895361ui64, 0x9A95597AAE5C905Dui64,
	0x6E1272E5BB93F93Fui64, 0x0E39E612402BFCF8ui64, 0x576C9E8CA2A3B35Eui64,
	0x7E2E629996D0C35Fui64, 0xC95DFF54E3524FCCui64, 0x260F9DEBDEB0E5CBui64,
	0x577B6C6640BAF1ABui64, 0xCA76677779CA358Eui64, 0x9E2714BEBCFDB144ui64,
	0xD660595CE30FD3EEui64, 0x72DE172D55A5706Eui64, 0xB4C84D564489D420ui64,
	0x160AA2B9399D5A9Dui64, 0x2906ECE619DAC4D2ui64, 0x12CE8E8E68A4C317ui64,
	0x6BE2DFE89901CAA1ui64, 0xEE1D68158102EB77ui64, 0x64EB75E45BDA1AC5ui64,
	0xEFECF9F98720B55Dui64, 0x41CDF813931315BFui64, 0x5F1E4F50CF98FFD4ui64,
	0xE69E09EED12E173Bui64, 0x89A3707F0396FF65ui64, 0x81E36B9DF4FFB492ui64,
	0x58C32E883D4DE6DDui64, 0x2D4725C2A5F0B469ui64, 0x6B2B9C27CC421CACui64,
	0x3C30F2AD966800C7ui64, 0xFF74938BB76B8A7Cui64, 0x52B5C99114FD93FAui64,
	0x51647EDCA6C104DAui64, 0xEB47684CF796DF4Eui64, 0x376D74A5AB14BD71ui64,
	0xF0871FEF8E9DAA3Eui64, 0x1D65B134B2E045B6ui64, 0x9DC8C0D8623BBA48ui64,
	0xAD6FC3C59DBDADF4ui64, 0x66F6EBA55488B569ui64, 0xB00D53E0E2D38F0Aui64,
	0x43A4212CEAD34593ui64, 0x44724185FF7019FFui64, 0x50F46061432B3635ui64,
	0x880AA4C24E6B320Bui64, 0xCAFCB3409A0DB43Fui64, 0xA7F1A13DEF47514Bui64,
	0x3DC8A385A698220Cui64, 0xFA17F82E30B85580ui64, 0x430E7F0E88655F47ui64,
	0x45A1566013837B47ui64, 0x84B2306D2292804Eui64, 0xE7A3AF21D074E419ui64,
	0x09D08E2C5E569D4Dui64, 0x84228F8908383FA2ui64, 0xC34079610C8D3E82ui64,
	0x66C96426C54A5453ui64, 0xD41F164117D32C93ui64, 0x7829A66BF1FEC186ui64,
	0x4BB6846694BDFC18ui64, 0x857D1C1C01352C01ui64, 0xAB8E68BA85402A45ui64,
	0x74B3C4F101FE76C8ui64, 0x6CF482CFAFB29FFEui64, 0x28B174A18F4DC3D1ui64,
	0x833C3425B2AA3755ui64, 0x8AA58A32747F4432ui64, 0xFE7B9FB4BCE3CD58ui64,
	0xB0836B2C16FA5553ui64, 0x1D08EE6861BF3F23ui64, 0x0FAE41E914562DF3ui64,
	0xB10A2E041937FC57ui64, 0xDA60BB363415BF4Cui64, 0xEEC67DBAB4CF4F0Aui64,
	0x9A6ED59FCC923B5Cui64, 0x9A913C01A8EC7A83ui64, 0xAD4779F2F9C7721Fui64,
	0x2BF0B7D105BE7459ui64, 0x189EFA9AD5195EC6ui64, 0xB5C9A2DD64B2A903ui64,
	0x5BCD642B2C2FD32Cui64, 0xFED3FBF78CB0891Fui64, 0x1ED958EE3C36DD3Fui64,
	0x030F5DE9CA65E97Cui64, 0xBB5BCF8C931B85FEui64, 0xFD128759EA1D8061ui64,
	0x2C0238AC416BE6BCui64, 0xBB018584EEACFA27ui64, 0xCEA7288C1964DE15ui64,
	0x7EA5C3840F29AA4Dui64, 0x5DA841BA609E4A50ui64, 0xE53AF84845985EB1ui64,
	0x93264DA9487183E4ui64, 0xC3A4E367AF6D8D15ui64, 0xDD4EB6450577BAF8ui64,
	0x2AA3093EE2C658ACui64, 0x3D036EC45055C580ui64, 0xDDEDB34341C5B7DFui64,
	0x524FFBDC4A1FAC90ui64, 0x1B9D63DE13D82907ui64, 0x69F9BAF0E868B640ui64,
	0xFC1A453A9253013Cui64, 0x08B900DECAA77377ui64, 0xFF24C72324153C59ui64,
	0x6182C1285C507A9Bui64, 0x4E6680A54A03CCC8ui64, 0x7165680200B67F1Fui64,
	0xC3290B26A07DCE5Bui64, 0x2AD16584AA5BECE9ui64, 0x5F10DF677C91B05Eui64,
	0x4BE1B0E2334B198Aui64, 0xEA2466E4F4E4406Dui64, 0x6ECAA92FF91E6F1Dui64,
	0x0267738EFA75CADDui64, 0x4282ED10A0EBFCF2ui64, 0xD3F84CE8E1685271ui64,
	0xB667ED35716CA215ui64, 0x97B4623D70DB7FA8ui64, 0xB7BA3AA5E6C2E7CBui64,
	0x8942B2F97118255Bui64, 0x009050F842FB52ADui64, 0x114F5511999F5BD5ui64,
	0x70C1CAAF1E83F00Aui64, 0xAC8EE25D462BB1AAui64, 0x63EEF42AD4E1BED9ui64,
	0x58DFBB3D22D3D1A5ui64, 0x82B0027C0C63D816ui64, 0x48D038F08F3D848Bui64,
	0xCE262D5F9A12610Eui64, 0xA54BF51C21BD0167ui64, 0xF3645F6FB948397Dui64,
	0x9188AE58532DA501ui64, 0xEC90B0E1479DB767ui64, 0x37F4886B83724F80ui64,
	0x232B8FF20ACD95AFui64, 0x88A228285D6BCDF0ui64, 0x321FB91600259AEEui64,
	0xA1F875F161D18E5Eui64, 0x5B6087CDA21AEA0Cui64, 0x0156923ED1A3D5F1ui64,
	0xC2892C8A6133B5D3ui64, 0x015CA4DF0EA6354Dui64, 0x5E25EB261B69A7D4ui64,
	0xAAA8CF0C012EFBA7ui64, 0xCF3466248C37868Bui64, 0x0D744514BD1D82C0ui64,
	0xB00FF1431EDDF490ui64, 0xC79B86A0E3A8AB08ui64, 0xFC361529BC9F1252ui64,
	0x869285653FB82865ui64, 0x9F1C7A17546339ABui64, 0xE31AA66DBD5C4760ui64,
	0x51B9D2A765E0FC31ui64, 0x31F39528C4CD13D8ui64, 0x16C6C35B0D3A341Dui64,
	0x90296B1B0F28E2CDui64, 0x36338472A8DB5830ui64, 0xA648E6D44DF14F87ui64,
	0x93E231E65EB1823Fui64, 0x95AA7B9D08E2B627ui64, 0x7932D149374700C7ui64,
	0x09EFE0A8BF245193ui64, 0x742AA63BCEAFD6D8ui64, 0x82D4BC5FEDF158B7ui64,
	0x02CDEA673CFF150Dui64, 0xD8D7B5813B602D15ui64, 0xA5A7B670EF15A5EDui64,
	0x4C08E580A1D46AF2ui64, 0xC3CA9B905D035647ui64, 0x6A39ABB02F6F1B83ui64,
	0xD2EC2169F4D02436ui64, 0x8E6AEA4DF8515AF2ui64, 0x7B3DD4A8693CA2DAui64,
	0xC2ABF17A50AEC383ui64, 0xD4FB84F8B6D4F709ui64, 0x2839A3EAA2E4C8A7ui64,
	0x5D5FD278FE10E1E9ui64, 0x5610DDF74125D5A7ui64, 0xA484B0B83461DCEAui64,
	0xA511920C0A502369ui64, 0xC53F30C6A5394CA4ui64, 0x528799285D304DD4ui64,
	0xF6D7914CB252BB48ui64, 0x892129CB52E65D15ui64, 0x15A81B70519ACE6Fui64,
	0x5CFBFFD7A2A1C630ui64, 0x3B900509C82DF46Dui64, 0x19C3CE05D66D5FFCui64,
	0x937D521A4A4799A0ui64, 0xD0AE34A6EAD7207Dui64, 0x3258A69F1D1A1B38ui64,
	0xB173E3255723CC02ui64, 0xD7E48FEF7F414F1Bui64, 0xDCEBA75F5C761ABEui64,
	0x6DA10C618DEA0D17ui64, 0x423FA8B05954FBD1ui64, 0x7E73C2E7D923F3C9ui64,
	0xC22E21C927C684D1ui64, 0x756BAA758764685Fui64, 0x8C90B4C4E741D880ui64,
	0x1B658C9F4B41D846ui64, 0x5D80C14094366707ui64, 0xB55FED3E03C00F2Dui64,
	0x9B69EB7964C69C83ui64, 0x356ED81C9494DADDui64, 0x7599AFF0B2A339D6ui64,
	0xA5EBFD25C9B5816Bui64, 0xA481DC1C8995E1EFui64, 0xE42C63DF0D402397ui64,
	0x3B497B4C30873BAAui64, 0xA950B78BA8772C96ui64, 0xD46308D4C76F115Dui64,
	0x73714A4ACA76A857ui64, 0x0DA86B958FF8CB7Dui64, 0xEB61F617B90E0A75ui64,
	0xD6106C9B39F51F55ui64, 0xB179F73A6BD23B59ui64, 0xE7F056E50104A460ui64,
	0xBC5B5387634A8642ui64, 0xE1678D8752996AF4ui64, 0xB508F3D394664A4Bui64,
	0xC88536DC4A219B0Fui64, 0x39964CBB8CE367B1ui64, 0xD51E211D5E9E1417ui64,
	0x6821B97B496870F2ui64, 0xA596257350CA0A99ui64, 0x6D051EE2C49D4D1Dui64,
	0xCB426AD61AA8D9B5ui64, 0x5FFD3A4062B06D22ui64, 0xDAD37BF2A4C594EBui64,
	0x6B9CC848E2B0C686ui64, 0x19B4232F3BC622AEui64, 0x70C13C7E5950B702ui64,
	0x383318CA622101ACui64, 0xD9647C028CD1C4DFui64, 0x006D123BC553B93Cui64,
	0x2CA9D7D896EAE722ui64, 0xF19872AC8A0BD5A8ui64, 0x59838578EB9E8E5Cui64,
	0xB948621EE99B27D4ui64, 0x2B470E6036E0E387ui64, 0xD0A7E8F0C8A32A84ui64,
	0xCBF869271A8E0914ui64, 0x705F76A5EA4437CFui64, 0xBAD2BF4933215152ui64,
	0xE52EDE847038EA23ui64, 0xB8A3EFD3D58D7607ui64, 0x748472F5AD330239ui64,
	0xCC079CFD428690F6ui64, 0x3687450CB7534DACui64, 0x0FEF82D5CC8ACE2Aui64,
	0x214653D5C552CA9Aui64, 0x9FCA4E87BF6A04FDui64, 0x78D4B114D234A0D7ui64,
	0x22840422BD6A5BB5ui64, 0x5B9ABE0DE1B4410Fui64, 0xB3B50007963FDD6Bui64,
	0x53A8A46793B68E35ui64, 0x8CDD8E8D188B6033ui64, 0x5DD22B6E3ED49572ui64,
	0xE561F5D27F5302D6ui64, 0xDF89CEC3322E56CDui64, 0x87167F503D600F90ui64,
	0x1698BB71C8201862ui64, 0xF7BF5DFDB023108Eui64, 0xA17FB15B66ACFB5Fui64,
	0x2DD771987768073Bui64, 0x19299D2D86E0EB29ui64, 0x8B537B7F206EED29ui64,
	0xE536DA153325ABFCui64, 0x30A69976796DB3B9ui64, 0x8E65A2C94E2D4981ui64,
	0xC301D53553BD6514ui64, 0x46DF3639B9E43790ui64, 0x3004CD0E5AFD0463ui64,
	0x46E460B0F6ACA1A0ui64, 0xCBA210E7372D9BD5ui64, 0x45064274A74CA582ui64,
	0xFDD57EA43CE631AEui64, 0xF2BA08FFA4A683D0ui64, 0x8DA658C4DAD42999ui64,
	0x7418042943E88040ui64, 0x96000F72E9371FEFui64, 0xE9F1212DC8F47302ui64,
	0x2AFB565ECC3553EDui64, 0xCD3D55137EFF7FD6ui64, 0xD36F11059388E442ui64,
	0xC4B47515DB5709DDui64, 0x5C363EFBF0BAAB67ui64, 0x28C63B5A31650BBBui64,
	0x6AE54E5068061C81ui64, 0xDEE62000F4E0AA21ui64, 0xE8238672FE088A8Bui64,
	0x9869CB6370F075B9ui64, 0xBA376E2FC7DB330Fui64, 0xB0F73E208487CDEEui64,
	0x359D5017BE37FE97ui64, 0x684D828C7F95E2DCui64, 0x9985ECA20E46AE1Fui64,
	0x8030A5137D1A21C4ui64, 0xF78CDC00FC37AC39ui64, 0x41CDDC8E61D9C644ui64,
	0xB6F3CD1D833BAD1Dui64, 0x301D0D858A23DE22ui64, 0xA51FCA12AD849BC8ui64,
	0x9F55E615986AB10Eui64, 0x904AAA59854F2215ui64, 0x12ECEA4AB40F51A7ui64,
	0xB4EDF5807735E23Bui64, 0x6190200F1C589478ui64, 0xA3CA57F321909A5Aui64,
	0x0BFAEE04B7325B63ui64, 0x10C393E7FBCF826Dui64, 0x4050A2CA53FDA708ui64,
	0xF31114A9B462B680ui64, 0x6FB9A6F121BA2006ui64, 0x04550CF09389D602ui64,
	0xB8C7D6D8CA8942F7ui64, 0x71BB430C6436E9D1ui64, 0xD9070DD5FAA0A10Aui64,
	0x8FD6827757D07E5Bui64, 0xD04E6C313F8FD974ui64, 0x2CFDEA1187909B9Aui64,
	0xC7A8E758C115F593ui64, 0xA79A17663009ACC2ui64, 0x8091A6B5372B141Dui64,
	0xEB33B08767F5BA73ui64, 0xDAC1F6823B6111C7ui64, 0x697DF90C3515611Bui64,
	0xCC1005F198761F48ui64, 0x5067E4F5303B45A1ui64, 0x04816D292A2D9AC2ui64,
	0x2949C7A0874DD5E9ui64, 0x25DB2547804CEE5Eui64, 0x7EDC3A8946D6F229ui64,
	0x00B586F67FD0C622ui64, 0x3CAE5798E40324E0ui64, 0x0A4F1437DE637164ui64,
	0x5F59B2B715871981ui64, 0x5D68FF31051E48FBui64, 0x0F2C369D73A2AA46ui64,
	0xB009C6B53DD23399ui64, 0xC366A81277084988ui64, 0x5AF0E0CA0711E730ui64,
	0x7AD831A9E9E854BAui64, 0x1DD5EDB0CA4E85AEui64, 0x54651209D259E9DDui64,
	0x3EBB1D9DAB237EADui64, 0xDA96989317AC464Cui64, 0xBFCB0F8FBC52C74Eui64,
	0x9597ACB9E27B692Eui64, 0x6F436B1643C95B23ui64, 0xB81A1253E1C3CD9Dui64,
	0x7B35F37E905EC67Eui64, 0x29CE62666EDA76DDui64, 0xFF2490DC1EC4014Dui64,
	0x2D4FF9124DD6B5C4ui64, 0xB9510FEC23E0E9D1ui64, 0x8BCDBC56541ED071ui64,
	0x5414E097C1B0CCB2ui64, 0x82BEF8213076F5C7ui64, 0xE9FC9A71BD512615ui64,
	0xCF15ECC39490DF5Aui64, 0x49FA9328D8EE97DBui64, 0x5F80FF0153BC2145ui64,
	0xF63BA156B55BCB02ui64, 0x0E3B9113109FDF36ui64, 0x8FCD6528F54EDE69ui64,
	0x5D6AE9C000054763ui64, 0x326D873633431FBBui64, 0x380E07FFCEF7A978ui64,
	0xDCAA09874A1DF230ui64, 0x601494F49F6D261Eui64, 0x856159486C9B60E3ui64,
	0x85C7F822D07089A5ui64, 0xAFFB99CF5AB836C2ui64, 0x241AD414FBBB956Bui64,
	0x0CFC042822831692ui64, 0x382B16D049727FF2ui64, 0x784F9997633C819Aui64,
	0x5C40ED725F6C390Aui64, 0x2CE78B7A3331BA9Cui64, 0x9C80636639963B41ui64,
	0x1B2D41C968355018ui64, 0xD189B57691FB60E4ui64, 0x3BD599A9DD85CE31ui64,
	0x46FC8E2EF0B9A77Cui64, 0x1A389E07D0075EA4ui64, 0x1622CA52401DF2ACui64,
	0x528F3FF9B7993BFAui64, 0xF16C176CCA292DDBui64, 0x6C154010961EF542ui64,
	0x04B78E92BF6C82DFui64, 0x7D9AFEA6ABB46072ui64, 0x3BC573291CBFFC2Eui64,
	0x277FFF096D567AF3ui64, 0x1CBEB86841A6F757ui64, 0xD0BCD49E76CA20A7ui64,
	0x25B6024756B1FE90ui64, 0xE685C04EF84881FBui64, 0xDCAB14B352FC442Eui64,
	0x4FF80A521719953Dui64, 0xD10425E411DBE94Bui64, 0x60998D0507D6E38Dui64,
	0x146AA432C981BD5Eui64, 0x1729A596282AAA41ui64, 0x152BE1A263BAF963ui64,
	0x15278DF497D254CAui64, 0xE4B5E9891E88A5DAui64, 0x087FA3472067D0ACui64,
	0xD99C2899A0AD9158ui64, 0x5040F234DC531236ui64, 0x9D7E1531259EEE90ui64,
	0x29AFB8B49391036Eui64, 0x84B619599642D68Eui64, 0xE838AAE0F249545Cui64,
	0x42D524BA8BB96959ui64, 0x9A5B3E817A20EE59ui64, 0x584F0530EC4C566Bui64,
	0xD6544FD14B47F945ui64, 0x3613FB3B553A7CDEui64, 0x284E92B56831AA56ui64,
	0xCEE89BA10E951A22ui64, 0x476806FA1A8A44E0ui64, 0xC84CEF151885C1DFui64,
	0x3DB1D5C1B0B73936ui64, 0x45D2D90FDF452388ui64, 0x038A7DD71BC5DD21ui64,
	0x2AC90C7422C56819ui64, 0x4742046638ECE0FBui64, 0x553B44360FC8495Dui64,
	0xC8DBA1CF3F9A6E97ui64, 0xF85919F494CAB021ui64, 0x1479455C2FF236AFui64,
	0x29BCAD159F7D018Dui64, 0x016DFF51CBA3BCC5ui64, 0x234BF8A77F6B1CF5ui64,
	0x20564C6F44F9E641ui64, 0x063A550C5AA50FA8ui64, 0xB063D0AAAA96DFECui64,
	0x3EC659DF42C092F8ui64, 0x29D4A76A5A5F7E09ui64, 0x65EFF3EE6E691D1Eui64,
	0xBD1634F5721CF799ui64, 0xE85BD016723B43FFui64, 0x5233E9E7AEA11022ui64,
	0x8C68852EA9039B4Cui64, 0x2C978ADBE885BC15ui64, 0x726615ED9D497550ui64,
	0x7C1E145EB8D2BD96ui64, 0xC2FEFB25935A5D71ui64, 0x9EE9C3E1C3DE416Fui64,
	0xFFD568A03E20E0B3ui64, 0xF53649AD90156F2Aui64, 0x0331B91DCE54E7EDui64,
	0x67CED5A86E99392Fui64, 0x16FC0A5815500B05ui64, 0x030392E8D24A7C00ui64,
	0x232E5E31DF32A7B5ui64, 0xCC8BF22B1947DF21ui64, 0x4EC2C72D9C1EEABDui64,
	0x0B1B79F45220E668ui64, 0xCC3CF0EE9C4A899Bui64, 0xFC260A60592EBC80ui64,
	0xC1989A0382CB03EDui64, 0x35FE679A6CD800B2ui64, 0x8A6B1ADE4FBB162Fui64,
	0xB0FD284563625295ui64, 0xCDCC1C7B2181D024ui64, 0x5B8BA0C895C0BB23ui64,
	0xA681FEA9ADCD43DBui64, 0x0FE30FB6876DE718ui64, 0x6DDD1E27B769C494ui64,
	0x83A1E58460FFE8BBui64, 0x8FAD6FD2DC90FF65ui64, 0x41BB28B81201CB24ui64,
	0xA148CE79B2597204ui64, 0x7CB87DF97BB477A6ui64, 0x9F79E6DED87DC688ui64,
	0xE044D84A6C758171ui64, 0x1A29E750D9EC4097ui64, 0x8445FC2B80C4A0F5ui64,
	0x5EFD9784AFED4ED2ui64, 0x344C252BD90EB0E4ui64, 0xEAD18D2E4418E5B5ui64,
	0x207EF4FFC260BD24ui64, 0xD2E5C3AE534EC538ui64, 0x2F5A59BF3D10E7E1ui64,
	0x9528E29266C2924Cui64, 0x0121B6BDAB45D138ui64, 0xADD0256ACBC771DDui64,
	0x7301769600C6C50Dui64, 0x3E7404EA8231D497ui64, 0xB39B3840B8D03117ui64,
	0x56EFCEDDEF5B6634ui64, 0xE6BE2C0D73B72098ui64, 0x5A2841A21A5E4959ui64,
	0xCFEB3586156DF6E0ui64, 0xD84F58901E2D65B8ui64, 0x79796786CCC59703ui64,
	0x13BFA9A94DD07696ui64, 0x7B63116A6B5458B6ui64, 0x1406628176C932E0ui64,
	0xDD7ACC4E97F91B1Aui64, 0xC82B8F84A56BDBE8ui64, 0x325D87D08ED8B0B0ui64,
	0x3F7847B1E82002DDui64, 0x4662900D2ADAF6BFui64, 0x12AE9F58561DB1D7ui64,
	0xA896E956A95CC074ui64, 0xAA4FA3A2F8BA4084ui64, 0x1D577E35F5DCA67Fui64,
	0x796FF2D75469DEC2ui64, 0xBD3F3F87E4DE894Eui64, 0x3666B2262DEBFB6Bui64,
	0x1E26D7AEEF976C2Eui64, 0x6BC3854F867AC4A0ui64, 0x743DBF8C2E95A821ui64,
	0xA62A76B9AE2E645Aui64, 0xB4D76561F40187C1ui64, 0xD3E5F23F9FA5DB25ui64,
	0x34B1F6B39E6A87E2ui64, 0x7DA5C3DFF7BE72CFui64, 0xFDF46B1BE80AD4F9ui64,
	0x0B21121CA9653B8Aui64, 0x1199CA9D0A90F21Aui64, 0x6021EA302D01E0BAui64,
	0x8101D063C05CF5D4ui64, 0xE2652410DFE78F23ui64, 0x84095ACF47C21A25ui64,
	0xD7E29A4DB2FD3A99ui64, 0x7793C0CB57959F93ui64, 0x94C605308B9E5AA7ui64,
	0x943DB1AC54165B8Fui64, 0xC1391A544C07447Cui64, 0x3FEF1A61F785D97Bui64,
	0x6DFCC3152478BDE4ui64, 0x312AFB0E1982933Aui64, 0xB8069C2605631ED3ui64,
	0x5A6076423430BED2ui64, 0x34E379F09E2D4F42ui64, 0x9167F5E4019573E3ui64,
	0x18F81157828D2A49ui64, 0xF4A8723B4955EAB8ui64, 0x0BE6C0ABFEA9E8A6ui64,
	0xC63ADCF2CEF25556ui64, 0xC5EBD3BEAE9F364Fui64, 0xA301D60CF5B6F2FCui64,
	0x8C606CA881D27A00ui64, 0x826FEE13B554C18Aui64, 0x8DF251716F10B776ui64,
	0xB2573A33AC7D94FFui64, 0xC0E771248CB7ABB9ui64, 0x753DD605DB38F4EAui64,
	0x21901664C3D92114ui64, 0xA408FCB7A1892612ui64, 0x3084FC64A03D6722ui64,
	0xC8C9D9569AD42A34ui64, 0x1FBFBAFC1694B383ui64, 0x1894280CC3F94ABEui64,
	0xE14C38A7BBB54651ui64, 0x23A48CC84A6EB704ui64, 0xD034ADC45AABEDBDui64,
	0xC93F2C21C973C766ui64, 0x66A8AEC11D743CC6ui64, 0xB4F72AA52E37C145ui64,
	0xB02834DF0D9266B4ui64, 0xDB8E724EA1FF402Fui64, 0x531E9C058112E352ui64,
	0xC2F692531DB317D2ui64, 0xEFC9586498D263A7ui64, 0x84F2C524D2F3ADB9ui64,
	0xAFAF02C27CF25D08ui64, 0x385873595F9CFC09ui64, 0x36DDA10D1A152B7Aui64,
	0x9F9B997A0DACFB55ui64, 0x10AB5EB5C4714882ui64, 0x7BA4E8703E22B7EEui64,
	0x0A2BFD558607BCC8ui64, 0x201D3706F74F8BA1ui64, 0x3DBD573B1358F02Eui64,
	0x5B37645FA93BCEBCui64, 0xC0166864BC1A7544ui64, 0x45C7AA5559FC65D7ui64,
	0xEFEA04AA83349B78ui64, 0x607859194F9E9FD8ui64, 0xA6B9AE5B53CF7710ui64,
	0x73B9142ACBC50821ui64, 0x8B7D67495887E65Cui64, 0x39B6C4FB2B232E56ui64,
	0xD212DB10E31D2A68ui64, 0x629AC0A3D263DC6Eui64, 0x6BC2E7FF912050BAui64,
	0xE0AD5A8FDB183F62ui64, 0xF05648134F0C6F0Fui64, 0x31E146F4AF980FDAui64,
	0x7FAF0078D84D62CCui64, 0xE13F044C2830D21Eui64, 0x49A047AD204B4C4Bui64,
	0xF3AFBE2237351A74ui64, 0x32826C9217BB07EDui64, 0xD4C3AEB099319B5Cui64,
	0x49CE5BD05B2B0F61ui64, 0x75DD36984DCBD0A2ui64, 0x84EC5D7C2F0AAC6Cui64,
	0x8E59CC9B9942EDDFui64, 0x89FF85DCDF9AE895ui64, 0x6F9EE0D8D9E8D414ui64,
	0x10E01A59058D3904ui64, 0x1DFAF567BFF55D2Eui64, 0x8DD6A18C03382CD4ui64,
	0xE12FD89A0CF58553ui64, 0xE245DA902C0C4F5Cui64, 0x8BE7566B9987520Dui64,
	0x4CA1C0A4B38A3098ui64, 0x81E45015BE618A72ui64, 0xA80E0344FF27EFDFui64,
	0xC98DAEC6DC5005BAui64, 0xF56873F3A958AE5Eui64, 0xDB88604670C794ACui64,
	0x4F68E448DDF6535Fui64, 0x3443DBF1CA6031A8ui64, 0x73DFA5DEEF409A41ui64,
	0xA7C556941F6643B2ui64, 0x424BC40D8C83D962ui64, 0x6F292A325B99B726ui64,
	0x6EECB1009717D65Eui64, 0x899BE4CE7BB2D8EEui64, 0x25285FED3E59781Dui64,
	0x14C5AEDD76E092D3ui64, 0x9BB5EE10567640AEui64, 0xCD62A1D43558FD06ui64,
	0x70A7B09FC5F39447ui64, 0xF10064AE92EFFB99ui64, 0xD55FA1A918A23082ui64,
	0xD03F28AD25C73A78ui64, 0x76CFFFEE094D8B0Eui64, 0x4FD5A46AD5A4B4CFui64,
	0x8F3A36F9D7BF87E3ui64, 0x64224315210625BEui64, 0x749A131B71B64350ui64,
	0x9034FF9DAC089F48ui64, 0xB58D3017E7321217ui64, 0x549D818937D5CE90ui64,
	0x903CE1452419E99Cui64, 0xFD052F0388DB2E76ui64, 0x7390051E3972480Eui64,
	0x5E5F6EC3F27B3679ui64, 0x3E3637D4D4EE917Dui64, 0x4FE04068CA2A4309ui64,
	0x98C9C17454AAE42Dui64, 0x659AE0BDB113BC21ui64, 0x4C0BDECB1511AF4Cui64,
	0x17048BFAEAC0006Dui64, 0x68F106AADAA64912ui64, 0x2286234ECEB7EAF0ui64,
	0x350CD42CAF697E51ui64, 0x8DCDE6D1FAC19A9Fui64, 0xF97E55A245A8A8A2ui64,
	0xAAE86B2092DA90A3ui64, 0x5123E878AA8AEF76ui64, 0x022B88B9694A55F6ui64,
	0xC4C1A9B1C0221985ui64, 0x20056D91DD5E0FFEui64, 0xF5BF61EC225C9843ui64,
	0x1A315A05BDCF4A31ui64, 0x5710A21A8DF4F15Fui64, 0x99BD1A0AF97AD027ui64,
	0x7602C5997AD4E12Cui64
};

void PP_GetTableIndices(const BYTE* pbPasswordUtf8, int nTableSize,
	std::vector<int>& vOut)
{
	ATLASSERT((nTableSize >= 2) && (nTableSize <= 0x10000));
	ATLASSERT((nTableSize % 64) == 0);

	BYTE pbHash[64];
	sha512_ctx sha;
	sha512_begin(&sha);
	sha512_hash(pbPasswordUtf8, szlen((const char*)pbPasswordUtf8), &sha);
	sha512_end(pbHash, &sha);

	vOut.clear();
	for(int i = 0; i < (64 / 2); ++i)
		vOut.push_back((((int)pbHash[i * 2] << 8) | (int)pbHash[i * 2 + 1]) % nTableSize);
}

bool PP_GetTableBit(const UINT64* pvTable, int iBit)
{
	return ((pvTable[iBit >> 6] & (0x1ui64 << (iBit & 0x3F))) != 0x0ui64);
}

bool PP_IsPopularChar(TCHAR tch)
{
	return (((tch >= _T('A')) && (tch <= _T('Z'))) || ((tch >= _T('a')) && (tch <= _T('z'))) ||
		((tch >= _T('0')) && (tch <= _T('9'))) || (tch == _T('_')) || (tch == _T('!')));
}

#pragma warning(push)
#pragma warning(disable: 4996) // SCL warning

bool IsPopularPassword(LPCTSTR lpPassword)
{
	if(lpPassword == NULL) { ATLASSERT(FALSE); return false; }

	LPTSTR lpLower = _TcsSafeDupAlloc(lpPassword);
	const size_t dwStrLen = _tcslen(lpLower);
	std::transform(lpLower, lpLower + dwStrLen, lpLower, ::tolower);

	for(size_t j = 0; j < dwStrLen; ++j)
	{
		if(!PP_IsPopularChar(lpLower[j]))
		{
			SecureZeroMemory((unsigned char*)lpLower, dwStrLen * sizeof(TCHAR));
			SAFE_DELETE_ARRAY(lpLower);
			return false;
		}
	}

	BYTE* pbUtf8 = _StringToUTF8(lpLower);

	std::vector<int> vIndices;
	PP_GetTableIndices(pbUtf8, PPC_TABLE_SIZE, vIndices);

	bool bPopular = true;
	for(size_t i = 0; i < vIndices.size(); ++i)
	{
		if(!PP_GetTableBit(PPC_TABLE, vIndices[i])) bPopular = false;
	}

	SecureZeroMemory(pbUtf8, szlen((const char*)pbUtf8));
	SAFE_DELETE_ARRAY(pbUtf8);
	SecureZeroMemory((unsigned char*)lpLower, dwStrLen * sizeof(TCHAR));
	SAFE_DELETE_ARRAY(lpLower);
	return bPopular;
}

#pragma warning(pop)
